package com.slipper.system.dictionary;

import java.util.List;
import java.util.stream.Stream;

import com.slipper.common.bo.Result;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.slipper.system.api.dictionary.bo.DictionaryBO;
import com.slipper.system.api.dictionary.bo.DictionaryDataBO;
import com.slipper.system.api.dictionary.service.IDictionaryDataSv;
import com.slipper.system.api.dictionary.service.IDictionarySv;
import com.slipper.common.constants.Constants;
import com.slipper.common.permit.PermitAll;
import com.slipper.common.tree.TreeInfo;
import com.slipper.common.tree.TreeUtil;
import com.slipper.common.util.ResultUtil;

/**
 * 字典信息
 *
 * @date 2019-10-03 14:00:51
 */
@RestController
@RequestMapping("/dictionary")
public class DictionaryController {
    @Resource
    private IDictionarySv dictionarySv;
    @Resource
    private IDictionaryDataSv dictionaryDataSv;


    @PostMapping("/queryBy")
    public String queryBy(@RequestBody DictionaryBO bo) {
        return ResultUtil.toJson(dictionarySv.queryBy(bo));
    }

    @PostMapping("/queryPage")
    public String queryPage(@RequestBody DictionaryBO bo) {
        return ResultUtil.toJson(dictionarySv.queryPage(bo));
    }

    @PostMapping("/queryTree")
    public String queryTree() {
        Result<List<DictionaryBO>> result = dictionarySv.queryList(new DictionaryBO());
        TreeInfo treeInfo = new TreeInfo("字典树");
        treeInfo.setRootId(Constants.ROOT_ID);
        treeInfo.setIdField("id");
        treeInfo.setPidField("parentId");
        treeInfo.setTextField("name");
        return ResultUtil.toJson(TreeUtil.buildTree(treeInfo, result.getData()));
    }

    @PostMapping("/save")
    public String save(@RequestBody DictionaryBO bo) {
        DictionaryBO qo = new DictionaryBO();
        qo.setEqCode(bo.getCode());
        Stream<DictionaryBO> stream = this.dictionarySv.queryList(qo).getData().stream()
                .filter((item) -> !item.getId().equals(bo.getId() == null ? Constants.ROOT_ID : bo.getId()));
        if (stream.findAny().isPresent()) {
            return ResultUtil.toErrorJson("字典编码已经存在[" + bo.getCode() + "]！");
        }

        if (bo.getParentId() == null) {
            bo.setParentId(Constants.ROOT_ID);
        }
        return ResultUtil.toJson(dictionarySv.save(bo));
    }

    @PostMapping("/delete")
    public String delete(@RequestBody DictionaryBO bo) {
        String[] ids = null;
        if (bo.getIds() != null) {
            ids = bo.getIds();
        } else if (bo.getId() != null) {
            ids = new String[] { bo.getId() };
        }
        if (ids == null) {
            return ResultUtil.toErrorJson("参数不正确！");
        }
        for (String id : ids) {
            DictionaryBO qo = new DictionaryBO();
            qo.setParentId(id);
            if (!this.dictionarySv.queryList(qo).getData().isEmpty()) {
                qo.setId(id);
                qo.setParentId(null);
                return ResultUtil.toJson(
                        "字典[" +
                                this.dictionarySv.queryBy(qo).getData().getName() +
                                "]其下有子字典，不允许删除！");
            }

            DictionaryDataBO dataQo = new DictionaryDataBO();
            dataQo.setDicId(id);
            if (!this.dictionaryDataSv.queryList(dataQo).getData().isEmpty()) {
                qo.setId(id);
                qo.setParentId(null);
                return ResultUtil.toJson(
                        "字典[" +
                                this.dictionarySv.queryBy(qo).getData().getName() +
                                "]其下有字典数据，不允许删除！");
            }
        }
        return ResultUtil.toJson(dictionarySv.delete(bo));
    }

    @PermitAll
    @PostMapping("/queryDicData")
    public String queryDicData(@RequestBody DictionaryBO bo) {
        return ResultUtil.toJson(dictionarySv.queryDicData(bo.getCode()));
    }

    @PermitAll
    @PostMapping("/queryAllDicData")
    public String queryAllDicData() {
        return ResultUtil.toJson(dictionarySv.queryAllDicData());
    }

    @PermitAll
    @PostMapping("/queryDicDataByCode")
    public String queryDicDataByCode(@RequestBody DictionaryBO bo) {
        if (bo.getCode() == null || bo.getCode().trim().isEmpty()) {
            return ResultUtil.toErrorJson("参数异常！");
        } else {
            return ResultUtil.toJson(dictionarySv.querySingleDicData(bo.getCode()));
        }
    }
}
